package algorithm.leetcode.simple;

/*
 编写一个函数，输入是一个无符号整数，返回其二进制表达式中数字位数为 ‘1’ 的个数（也被称为汉明重量）。
 */

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/10/19
 */
public class Question191 {

    public static void main(String[] args) {
        int n = 00000000000000000000000000001011;
        Question191 question = new Question191();
        int count = question.hammingWeight(n);
        System.out.println(count);
    }

    public int hammingWeight(int n) {
        int count = 0;
        int mask = 1;
        for (int i = 0; i < 32; i++) {
            if ((n & mask) != 0) {
                count++;
            }
            // 关键点 从最后面往前面比较  0001 -> 0010 -> 0100 -> 1000
            mask <<= 1;
        }
        return count;
    }

}
